<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
session_start(); 
}  
include '../../operations/connection.php';

$page_start = $_REQUEST['page'];
$idcomp = $_REQUEST['idcomp'];
$newlang = $_REQUEST['newlang'];

if ($_REQUEST['newlang'] == "EN") {
	include "../../value/value_en.php";
} else if($_REQUEST['newlang'] == "VN") {
	include "../../value/value_vn.php";
}
else {
	include "../../value/value_kr.php";
}

$page=1;
$rows_per_page = 10 ;
$page_start = ( $page - 1 ) * $rows_per_page; 
$page_end = $page * $rows_per_page;

$a= (string)(microtime(true)*1000);
$b=split('\.',$a);
$partnerID= "UC".$b[0];

$parnername = $_REQUEST['parnername'];
$password = strtoupper(md5($_REQUEST['password']));
$databasename = $_REQUEST['databasename'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$website = $_REQUEST['website'];
$domainname = $_REQUEST['domainname'].".myapprove.com";
$roothome = $_REQUEST['roothome'];

//$keynametemp = explode(".",$domainname);
//if ($_REQUEST['domainname']==""){
//	$keyname = "elisoft";
//}else{
	$keyname = $_REQUEST['domainname'];
//}

if(isset($_REQUEST['register']) && $_REQUEST['register']=="add"){
	if(isset($_SESSION['checkdb_domain']) && $_SESSION['checkdb_domain']=="okk"){
		$insertpartner = "insert into ac_eli_manager(PartnerID,PartnerName,Email,Phone,Website,UserName,Password,DatabaseName,Role,Status,DomainName,KeyName,RootHome)
		VALUES('$partnerID','$parnername','$email','$phone','$website','admin','$password','$databasename',2,2,'$domainname','$keyname','$roothome');";
	mysql_query($insertpartner);

	set_time_limit( 0 );

	$filename = '../../database.sql';
	//$mysql_host = '1.234.53.52';
	//$mysql_username = 'cam';
	//$mysql_password = '5678';
	$mysql_database = $databasename;

	//mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
	mysql_query("Create database $mysql_database DEFAULT CHARACTER SET  'utf8' DEFAULT COLLATE 'utf8_general_ci';");
	mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

	$templine = '';
	$lines = file($filename);
	foreach ($lines as $line_num => $line) {
		if (substr($line, 0, 2) != '--' && $line != '') {
			$templine .= $line;
			if (substr(trim($line), -1, 1) == ';') {
				mysql_query($templine) or print('Error performing query \'<b>' . $templine . '</b>\': ' . mysql_error() . '');
				$templine = '';
			}
		}
	}

	$p="
	CREATE   PROCEDURE getTotalDocument(IN idemployee varchar(15))
	BEGIN
		select (SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as RePendingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 2 )  as ReNotApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 1 AND CONVERT(b.IDLabel USING UTF8) < 1
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as ReApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 3 AND CONVERT(b.IDLabel USING UTF8) < 1
			AND CONVERT(b.IDStatus USING UTF8) = 3 )  as ReDeclinedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc p INNER JOIN list_reference l ON
			CONVERT(p.IDGroupApprove USING UTF8) = CONVERT(l.id_group USING UTF8)
			WHERE CONVERT(l.id_employee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(p.IDTypeDoc USING UTF8)   != 11 AND CONVERT(l.IDLabel USING UTF8) < 1
			AND CONVERT(l.id_status USING UTF8)= 1)  as ReRefApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc p INNER JOIN list_reference l ON
			CONVERT(p.IDGroupApprove USING UTF8) = CONVERT(l.id_group USING UTF8)
			WHERE CONVERT(l.id_employee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(p.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(l.id_status USING UTF8)= 2)  as ReRefWaitingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8) = 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 2 )  as ReMeetingDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 2
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 ) as MySendingDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 4
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MySavedDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 1 AND CONVERT(a.IDLabel USING UTF8) < 1
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyApproveDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 3 AND CONVERT(a.IDLabel USING UTF8) < 1
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyDeclinedDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 2
			AND CONVERT(a.IDTypeDoc USING UTF8) = 11
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyMeetingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 6
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as BinReceiverDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 6
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as BinSendingDoc
		;
	END;
	";
	
	mysql_query($p);
	
	
	mysql_query("INSERT INTO employee(IDEmployee,UserName,Password, 
			FullName,IDManager,IDRole,Email,Address,Position,
			Mobile,State) VALUES(1,'admin','$password','admin',1,1,'$email','',1,'$phone',1);");
	}

}
	
//$connection = mysql_connect("1.234.53.52", "cam", "5678" ) ;
$connection = mysql_connect("localhost", "root", "root" ) ;
mysql_select_db("elihan", $connection);
mysql_query("set names 'utf8'");
$sqlpartner = mysql_query("SELECT am.PartnerID,am.PartnerName,am.DomainName,am.DatabaseName,am.Phone,am.Email,am.Website,am.`Status`
		FROM `ac_eli_manager` am WHERE am.Role=2 and am.`Status` != 3; ");
$number_of_page = ceil ( mysql_num_rows( $sqlpartner ) / $rows_per_page ); 
?>
<form action="#" name="formCheck" method="post">
				<table id="listRequest" class="rf-dt" style="width: 100%;">
					<thead class="rf-dt-thd">
						<tr class="rf-dt-shdr">
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 3%; font-size: 12px;">
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 20%; font-size: 12px;"><?php echo $l_doitac?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 15%; font-size: 12px;"><?php echo $l_domainame?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 13%; font-size: 12px;"><?php echo $l_databasename?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 9%; font-size: 12px;"><?php echo $l_phone?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 15%; font-size: 12px;"><?php echo $l_email?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 15%; font-size: 12px;"><?php echo $l_Website?>
							</th>
							<th class="rf-dt-shdr-c" scope="col"
								style="background-color: #eeeeee; width: 23%; font-size: 12px;"><?php echo $l_status?>
							</th>
						</tr>
						<?php
						if(mysql_num_rows($sqlpartner)>0){
							$i=0;
							while ($row = mysql_fetch_array($sqlpartner)){
								if ( $i >= $page_start ){
									?>
						<tr>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 3%; font-weight: normal; font-size: 12px;">
								<input type="checkbox" name="checkname"
								id="<?php echo 'checkcus'.$i?>"
								onclick="processCheck(this.id,document.formCheck.checkname);"
								value="<?=$row[PartnerID]?>" />
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 20%; font-weight: normal; font-size: 12px;"><?php echo $row[PartnerName]?>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 15%; font-weight: normal; font-size: 12px;">
								<a href="<?php echo "http://".$row[DomainName]?>" target="_blank"><?php echo $row[DomainName]?></a>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 13%; font-weight: normal; font-size: 12px;"><?php echo $row[DatabaseName]?>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 9%; font-weight: normal; font-size: 12px;"><?php echo $row[Phone]?>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 15%; font-weight: normal; font-size: 12px;"><?php echo $row[Email]?>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 15%; font-weight: normal; font-size: 12px;">
								<a href="<?php echo "http://".$row[Website]?>" target="_blank"><?php echo $row[Website]?></a>
							</td>
							<td class="rf-dt-shdr-c" scope="col"
								style="background-color: #ffffff; width: 23%; font-weight: normal; font-size: 12px;">
								<?php if($row[Status]==2){ echo $l_danghoatdong;}else{echo $l_ngunghoatdong;}?>
							</td>
						</tr>
						<?php
								}
								$i++;
								if ($i >= $page_end){
									break;
								}
							}
						}?>

<tr>
						<td colspan="8" class="rf-dt-shdr-c">
							<span style="background:#fff; padding:1px; display: inline-block;">
														
								<?php if ( $number_of_page > 1 ) 
								{ 
									if ($page==1)
										$p=2;
									else if ($page==$number_of_page)
										$p=$number_of_page-1;
									else $p=$page;
									if($page!=1){?>
										<a onclick="pagingcomp(1);" ><span class='button_next'><?php echo "<<<";?></span> </a>
										<a onclick="pagingcomp(<?=($page-1)?>);" ><span class='button_next'><?php echo "<<";?></span> </a>
									<?php  }
									else{
										echo "<span class='button_next'><<<</span> "; 
										echo "<span class='button_next'><<</span> ";
									}
									for ( $i = $p-1; $i <= $p+1; $i++ ) {	
											if ($i<=$number_of_page && $i>0)
											
												if ($i==$page){
													echo "<span class='button_next'><b>".$i."</b></span>"; 
												}else{
												?>
													<a onclick="pagingcomp(<?=$i?>);" ><span class="button_next">
													<?php 
													echo $i;
													?>
													</span> </a>
													<?php 
												}
											?>
										
									<?php 
											}
										
										if($page!=$number_of_page){ ?>
											<a onclick="pagingcomp(<?=($page+1)?>);"><span class='button_next'><?php echo ">>";?></span> </a>
											<a onclick="pagingcomp(<?=($number_of_page)?>);" ><span class='button_next'><?php echo ">>>";?></span> </a>
										<?php }
										else{
											echo "<span class='button_next'>>></span> ";
											echo "<span class='button_next'>>>></span> ";
										}
									}
								
									
									?>
																						
							</span>
						</td>
					</tr>
					</thead>
				</table>
			</form>
			
			<?php 
			mysql_close($connection);
			ob_flush();
			?>